/**
 * 获取控件列表的层级链
 * @param {*} widgetListFlat 控件列表(扁平化)(可通过getWidgetListFlat方法获取)
 * @returns {
 *   widgetId: [widgetId, ...]
 * }
 */
const getWidgetHierarchys = (widgetListFlat = {}) => {
    const hierarchys = {}
    const widgetsFlat = widgetListFlat
    if (Object.keys(widgetsFlat).length > 0) {
        const getWidgetHierarchys = (id, list) => {
            const { widgetData, parentId } = widgetsFlat?.[id] || {}
            if (widgetData !== undefined) {
                list.push(widgetData.eles.id)
                getWidgetHierarchys(parentId, list)
            }
            return list
        }
        for (const key of Object.keys(widgetsFlat)) {
            const { widgetData, parentId } = widgetsFlat[key]
            const list = [widgetData.eles.id]
            hierarchys[key] = getWidgetHierarchys(parentId, list).reverse()
        }
    }
    return hierarchys
}

export default getWidgetHierarchys